﻿using System;

namespace Emerald
{
    public struct BoardPoint : IEquatable<BoardPoint>
    {
        public byte X;
        public byte Y;
        public bool Set;

        public BoardPoint(byte x, byte y)
        {
            this.X = x;
            this.Y = y;
            this.Set = true;
        }

        public float EuclidianDistanceTo(BoardPoint bp)
        {
            return (float)Math.Sqrt(Math.Pow(this.X - bp.X, 2) + Math.Pow(this.Y - bp.Y, 2));
        }

        public int ManhattanDistanceTo(BoardPoint bp)
        {
            return Math.Abs(this.X - bp.X) + Math.Abs(this.Y - bp.Y);
        }

        public static bool operator ==(BoardPoint first, BoardPoint second)
        {
            return (first.X == second.X && first.Y == second.Y);
        }
        public static bool operator !=(BoardPoint first, BoardPoint second)
        {
            return false == (first == second);
        }

        internal int AsIndex
        {
            get { return Y * 21 + X; }
        }

        public override string ToString()
        {
            return Set ? string.Format("({0},{1})", X, Y) : "default";
        }

        public bool Equals(BoardPoint other)
        {
            return other.X == X && other.Y == Y && other.Set == Set;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof(BoardPoint)) return false;
            return Equals((BoardPoint)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = X.GetHashCode();
                result = (result * 397) ^ Y.GetHashCode();
                result = (result * 397) ^ Set.GetHashCode();
                return result;
            }
        }
    }
}
